package com.first.dao.impl;

import com.first.dao.UserDao;
import com.first.entity.User;
import com.first.util.DBUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Repository;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class UserDaoImpl implements UserDao {
    /**
     * 用户登录验证
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public User authentication(String username, String password) {
        String sql = "select * from user where username=? and password=?";
        List<Map> list = DBUtil.queryListMap(sql, username,password);
        User user = new User();
        if (list.size() > 0) {
            try {
                BeanUtils.populate(user, list.get(0));
                String queryDeptSql="select status from deptn  where deptid=?";
                String queryRoleSql="select status from role where rid=?";
                List<List> deptList = DBUtil.queryList(queryDeptSql,user.getDeptid());
                List<List> roleList = DBUtil.queryList(queryRoleSql,user.getRid());
                int deptStatus = Integer.parseInt(deptList.get(0).get(0).toString());
                int roleStatus = Integer.parseInt(roleList.get(0).get(0).toString());
                user.setDeptStatus(deptStatus);
                user.setRoleStatus(roleStatus);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return user;
        }
        return null;
    }

    /**
     * 添加员工信息
     * @param user
     * @return
     */
    @Override
    public int addMainUser(User user) {
        String verify="select * from user where card=? or username=?";
        List<List> list = DBUtil.queryList(verify, user.getCard(), user.getUsername());
        if(list.size()>0){
            return -2;
        }
        String sql = "insert into user values(null,?,123456,null,?,?,?,?,null,?,?,?,null,?,null,?)";
        return DBUtil.update(sql, user.getUsername(), user.getEmail(), user.getPhone(),user.getSex(),user.getAddress(),user.getDeptid(),user.getStatus(),user.getRid(),new Date(),user.getCard());
    }
    /**
     * 修改员工的详细信息
     * @param user
     * @return
     */
    @Override
    public int update(User user) {
        String sql = "update user set name=?,email=?,phone=?,address=?,url=? where id=?";
        return DBUtil.update(sql, user.getName(), user.getEmail(), user.getPhone(),  user.getAddress(), user.getUrl(), user.getId());
    }

    /**
     * 重置密码
     *
     * @param id
     * @return
     */
    @Override
    public int resetPasswordById(Integer id) {
        String sql = "update user set password=123456 where id=?";
        return DBUtil.update(sql, id);
    }


    /** 查询全部用户信息
     * @param user
     * @return
     */
    @Override
    public List<Map> queryAllUser(User user) {
        StringBuffer sql = new StringBuffer("select u.id,u.username,u.email,u.phone,u.sex,u.address,d.dept, r.rname,u.rid,u.deptid,u.status,u.entrydate,u.card from (user u LEFT JOIN deptn d on u.deptid=d.deptid ) LEFT JOIN role r on  u.rid=r.rid where u.status!=1");
        if(user.getUsername()!=null){
            sql.append(" and u.username like '%"+user.getUsername()+"%'");
        }
        if(user.getDeptid()!=null){
            sql.append(" and u.deptid="+user.getDeptid());
        }
        if(user.getRid()!=null){
            sql.append(" and u.rid ="+user.getRid());
        }
        sql.append(" limit ?,?");
        List<Map> list = DBUtil.queryListMap(sql.toString(),(user.getPage() - 1) * user.getLimit(), user.getLimit());
        return list;
    }

    @Override
    public int queryAllUserCount() {
        String sql="select count(*) from user";
        List<List> list = DBUtil.queryList(sql);
        return Integer.parseInt(list.get(0).get(0).toString());
    }
    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Override
    public int updateUser(User user) {
        String sql="update user set email=?,phone=?,card=?,address=?,sex=?,deptid=?,status=?,rid=? where id=?";
        int i = DBUtil.update(sql, user.getEmail(), user.getPhone(), user.getCard(), user.getAddress(), user.getSex(), user.getDeptid(), user.getStatus(), user.getRid(), user.getId());
        if(user.getStatus()==1){
            String sqls="update user set resignationdate=? where id=?";
            DBUtil.update(sqls, new Date(),user.getId());
        }
            return i;
    }
    @Override
    public List<Map> getResign() {
        String sql = "select u.id,u.username,u.email,u.phone,u.sex,u.address,d.dept, r.rname,u.status,u.resignationdate,u.entrydate,u.card from (user u LEFT JOIN deptn d on u.deptid=d.deptid ) LEFT JOIN role r on  u.rid=r.rid where u.status=1";
        List<Map> list = DBUtil.queryListMap(sql);
        if(list.size()>0){
            return list;
        }
        return null;
    }

    @Override
    public int changePassword(String oldPassword, String newPassword, Integer id) {
        String sql="update user set password=? where id=? and password=?";
        System.out.println(id);
        int i = DBUtil.update(sql,newPassword, id, oldPassword);
        System.out.println(i);
        return i;
    }
}
